package com.fosdata.dpm.controller;



import cn.hutool.cache.CacheUtil;
import com.fosdata.dpm.domain.SysUser;
import com.fosdata.dpm.entity.dto.ApiResult;
import com.fosdata.dpm.exception.AuthFailedException;
import com.fosdata.dpm.services.SysUserServices;
import com.fosdata.dpm.util.CaptchaUtil;
import com.google.common.cache.Cache;
import org.apache.commons.lang3.StringUtils;
import org.apache.el.util.ConcurrentCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author l
 * @since 2025-10-19
 */
@RestController
public class SysUserController {

    private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    private SysUserServices sysUserServices;
    // 注入自定义的缓存实例
    @Autowired
    private Cache<String, String> userCache; // 注入缓存实例

    /**用户登陆**/
    @PostMapping("user/login")
    public ApiResult userLogin(HttpServletRequest request,String phone,String password,String code){

        try {
            if (StringUtils.isAnyEmpty(code,phone,password)) {
                return ApiResult.error("验证码为空");
            }
            String captcha =userCache.getIfPresent(code);
            logger.info("获取前端传输的验证码---"+code+"   缓存中取到的验证码  "+ captcha+ "");
            if(StringUtils.isEmpty(captcha) || !code.equals(captcha)){
                return ApiResult.error("验证码不正确");
            }

            SysUser user = sysUserServices.queryuserAndpasswd(phone,password);
            if (user == null){
                return ApiResult.error("用户登录失败");
            }
            return ApiResult.ok(user.getPhone());
        }catch (Exception e){
            e.printStackTrace();
        }
        return ApiResult.error("用户登录失败");

    }

    /**新增用户**/
    @PostMapping("user/add")
    public ApiResult userAdd( String username, String password,  String phone){
        int i = sysUserServices.addSysUser(username,password,"",phone,"");
        ApiResult apiResult  = i > 0? ApiResult.ok():ApiResult.error();
        return apiResult;
    }

    /**验证码**/
    @GetMapping("user/captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 禁止缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 设置响应类型为图片
        response.setContentType("image/jpeg");
        // 生成验证码并输出到响应流
        logger.info("支持的图片格式"+ Arrays.toString(ImageIO.getWriterFormatNames()));
        String code = CaptchaUtil.generateCaptcha(response.getOutputStream());
        // 将验证码存入Session，用于后续验证
        logger.info("------------------"+ code);
        userCache.put(code,code);
    }
}
